/**
 * 页面mixin
 */
import {
	userInfo
} from "os";
import {
	toLogin
} from '@/libs/login.js';
import {
	isWeixin
} from "../utils";
import eHealth from "@/libs/eHealth.js";
import * as api from '@/api/api.js';
import * as config from "@/api/config.js";
import * as ArticleApi from "@/api/cms/article.js";
import { encrypt, decrypt } from '../utils/encrypt';
import {
	mapGetters
} from "vuex";
import parser from "@/hosp/components/jyf-parser/jyf-parser.vue";
import barcode from 'barcode'
import pBox from '@/hosp/components/patient-box.vue'
import pCode from '@/hosp/components/patient-code.vue'
import inBox from '@/hosp/components/inhospital-box.vue'
import NavBtn from '@/hosp/components/nav-fixed-button.vue'
import NavAdd from '@/hosp/components/nav-fixed-add.vue'
import dawnPagination from '@/hosp/components/dawn-pagination.vue'
import dawnBar from '@/hosp/components/dawn-bar.vue'
import dawnMsg from '@/hosp/components/dawn-msg.vue'
import dawnLoading from '@/hosp/components/dawn-loading.vue'
import selectDic from '@/hosp/components/selectDic.vue'
import StatusView from '@/hosp/components/statusView.vue'
import dawnSearch from '@/hosp/components/dawn-search.vue';
const app = getApp();
module.exports = {
	computed: mapGetters(['isLogin', 'userInfo','appConfig','patientInfo']),
	components: {
		barcode,
		parser,
		pBox,
		pCode,
		inBox,
		NavBtn,
		NavAdd,
		dawnBar,
		dawnPagination,
		dawnMsg,
		dawnLoading,
		selectDic,
		StatusView,
		dawnSearch,
	},
	data() {
		return {
			navTitle: ''
		}
	},
	async onLoad(options) {
		console.log("mixin onLoad")
		// console.table(options)
		// if (options.hasOwnProperty('navTitle')) {
		// 	this.navTitle = options.navTitle
		// 	uni.setNavigationBarTitle({
		// 		title: options.navTitle
		// 	});
		// }
		// 更新顶部导航栏信息
		this.updateCustomBarInfo();
		await this.getRunConfig();
	},
	methods: {
		getRunConfig() {
			console.info("app.globalData.closeing:"+app.globalData.closeing);
			if(!app.globalData.closeing){
				api.getConfigByKey(config.CONFIG_TYPE.CLOSE_CONFIG).then(res => {
					console.table(res);
					if (res.code === 0 && res.data.visible) {
						uni.navigateTo({
							url: '/hosp/message'
						})
					}else{
						app.globalData.closeing = true;
					}
				}).catch(err => {
				
				})
			}
			
		},
		addEHealth(){
			let urlData = location.pathname;
			eHealth.toList(urlData);
		},
		// 点击左上角首页按钮时触发事件
		goHome() {
			console.log("goHome");
			uni.switchTab({
				url: '/main/index/index'
			})
		},
		// 点击左上角返回按钮时触发事件
		goBack() {
			console.log("goBack");
			// 通过判断当前页面的页面栈信息，是否有上一页进行返回，如果没有则跳转到首页
			const pages = getCurrentPages()
			if (pages && pages.length > 0) {
				const firstPage = pages[0]
				if (pages.length == 1 && (!firstPage.route || firstPage.route != 'main/index/index?init=no')) {
					uni.switchTab({
						url: '/main/index/index?init=no'
					})
				} else {
					uni.navigateBack({
						delta: 1
					})
				}
			} else {
				uni.switchTab({
					url: '/main/index/index?init=no'
				})
			}
		},
		// 切换导航
		switchTabbar(index) {
			console.log("切换导航:" + index)
			var menu = this.tabbarList[index];
			// uni.redirectTo({
			// 	url: menu.url+'?init=no'
			// })
			uni.switchTab({
				url: menu.url
			})
		},
		login() {
			toLogin();
		},
		navTab(url) {
			if (this.userInfo) {
				uni.switchTab({
					url: url
				});
			} else {
				this.$tn.message.toast('用户未登录，请先登录！', true);
			}
		},
		nav(item) {
			console.table(item);
			if (true) {
				
				// if(this.userInfo.status === 0){
				var url = '';
				if (item.url) {
					// url = item.url + '?navTitle=' + item.titleSub
					url = item.url;
				} else {
					url = item;
				}
				// }else{
				// 	this.$tn.message.toast('用户未开启，请联系管理员！', true);
				// }
				
				uni.showLoading({
				  title: '正在处理中...',
				  mask: true, // 是否显示透明蒙层，防止触摸穿透
				});
				
				if(url.indexOf('http')!==-1){
					if(item.color === 'pid'){
						url = url+this.patientInfo.patientId
					}
					location.href = url;
				}else {
					uni.navigateTo({
						url: url
					});
				}
			} else {
				this.$tn.message.toast('用户未登录，请先登录！', true);
				toLogin();
			}
		},
		navNoAuth(url) {
			uni.showLoading({
			  title: '正在处理中...',
			  mask: true, // 是否显示透明蒙层，防止触摸穿透
			});
			if(url.indexOf('http')!==-1){
				location.href = url;
			}else {
				uni.navigateTo({
					url: url
				});
			}
		},
		newsDteail(item) {
			console.table(item);
			uni.showLoading({
			  title: '正在处理中...',
			  mask: true, // 是否显示透明蒙层，防止触摸穿透
			});
			if(item.linkUrl){
				ArticleApi.addArticleBrowseCount(item.id);
				location.href = item.linkUrl;
			}else {
				uni.navigateTo({
					url: '/hosp/news/detail?id=' + item.id
				})
			}
		},
		openLink(url){
			uni.showLoading({
			  title: '正在处理中...',
			  mask: true, // 是否显示透明蒙层，防止触摸穿透
			});
			window.location.href = url;
		},
		toPayUI(orderNumber,isChsPay){
			console.info(orderNumber);
			console.info(isChsPay);
			// this.$util.setStorageData(config.STORE_KEY.ORDER_NO, this.orderNumber)
			// let orderNo =  encrypt(orderNumber);
			let url = '/hosp/order/info?orderNumber=' + orderNumber+'&isChsPay='+isChsPay
			
			if (uni.getSystemInfoSync().platform == 'ios') {
				// uni.redirectTo({
				//     url: url
				// })
				uni.navigateTo({
					url: url,
					// #ifdef H5
					success:(res)=>{
						if(res.errMsg == 'navigateTo:ok'){
							window.location.reload(true); 
							if (location.href.indexOf("?#") < 0) {
								 location.href = location.href.replace("#", "?#");
							}
						}
					}
					// #endif
				})
			} else {
				uni.navigateTo({
				    url: url
				})
			}
		},
		updateNavBar(options) {
			// if (options.navTitle) {
			// 	uni.setNavigationBarTitle({
			// 		title: options.navTitle
			// 	});
			// }
		},
		//拨打电话
		callPhoneNumber(number) {
			uni.makePhoneCall({
				phoneNumber: number,
			});
		},
		copyData(data) {
			uni.setClipboardData({
				data: data,
			})
		},
		float0(price){
			return this.$util.toFloat0(price);
		},
		float1(price){
			return this.$util.toFloat1(price);
		},
		float2(price){
			return this.$util.toFloat2(price);
		},
		// 更新顶部导航栏信息
		async updateCustomBarInfo() {
			// 获取vuex中的自定义顶栏的高度
			let customBarHeight = this.vuex_custom_bar_height
			let statusBarHeight = this.vuex_status_bar_height
			// 如果获取失败则重新获取
			if (!customBarHeight) {
				try {
					const navBarInfo = await this.$tn.updateCustomBar()
					customBarHeight = navBarInfo.customBarHeight
					statusBarHeight = navBarInfo.statusBarHeight


				} catch (e) {
					setTimeout(() => {
						this.updateCustomBarInfo()
					}, 10)
					return
				}
			}

			// #ifdef H5
			customBarHeight = '40'
			// #endif

			// 更新vuex中的导航栏信息
			this.$tn.vuex('vuex_status_bar_height', statusBarHeight)
			this.$tn.vuex('vuex_custom_bar_height', customBarHeight)
		}
	}
}